package com.wego.controller.manager;

import com.wego.controller.manager.base.BaseController;
import com.wego.service.base.BaseService;
import com.wego.common.bean.PageBean;
import com.wego.common.bean.ResultBean;
import com.wego.common.enumeration.ResultEnum;
import com.wego.common.utils.QueryUtil;
import com.wego.common.utils.ResultBeanUtil;
import com.wego.converter.trans.AddressTrans;
import com.wego.entity.domain.Address;
import com.wego.entity.query.AddressQuery;
import com.wego.entity.vo.manager.AddressVO;
import com.wego.service.AddressService;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


/**
 * 地址控制器
 *
 * @author hc
 */
@Controller
@RequestMapping("/manager/address")
public class AddressController extends BaseController<Address> {
    /**
     * 服务对象
     */
    @Autowired
    private AddressService addressService;

    @Autowired
    private AddressTrans addressTrans;

    /**
     * 不提供默认的构造方法，限制其子类必须调用该方法为baseService赋值
     * @param service
     */
    public AddressController(BaseService<Address> service) {
        super(service);
    }

    /**
     * 省份列表
     *
     * @param addressQuery
     * @param model
     * @param resultBean
     * @return
     */
    @GetMapping("/list")
    public String list(AddressQuery addressQuery, Model model, @ModelAttribute("resultBean") ResultBean resultBean) {
        //构造动态查询条件
        String keywords = addressQuery.getKeywords();
        if (keywords != null) {
            if (addressQuery.getReceiver() == null) {
                addressQuery.setReceiver(keywords);
            }
            if (addressQuery.getAddr() == null) {
                addressQuery.setAddr(keywords);
            }
            if (addressQuery.getPhone() == null) {
                addressQuery.setPhone(keywords);
            }
        }

        final PageBean<Address> addressPageBean = addressService.selectPage(addressQuery);
        //转换
        PageBean<AddressVO> pageBean = addressTrans.getInstance().tPageBean2VPageBean(addressPageBean);

        //分页时携带上查询条件
        String params = QueryUtil.genQueryCondition(addressQuery);
        pageBean.setUrl("manager/address/list?" + params);
        model.addAttribute("pageBean", pageBean);

        //查询条件数据回显时使用
        model.addAttribute("addressQuery", addressQuery);

        //增删改操作完返回到列表页面的提示信息
        if (resultBean != null) {
            model.addAttribute("resultBean", resultBean);
        }

        return "manager/address";
    }

    /**
     * 具体执行添加修改操作
     *
     * @param address
     * @return
     */
    @PostMapping("/doAddUpdate")
    public String doAddUpdate(Address address, RedirectAttributes redirectAttributes) {
        address.setUpdateTime(LocalDateTime.now());

        ResultBean resultBean;
        Long id = address.getId();
        if (id != null) {
            //修改
            boolean res = addressService.updateByPrimaryKeySelective(address);
            resultBean = ResultBeanUtil.build(res, ResultEnum.UPDATE_SUCCESS, ResultEnum.UPDATE_EXCEPTION);
        } else {
            //添加
            address.setCreateTime(LocalDateTime.now());
            boolean res = addressService.insertSelective(address);
            resultBean = ResultBeanUtil.build(res, ResultEnum.INSERT_SUCCESS, ResultEnum.INSERT_EXCEPTION);
        }

        //返回列表页面时，对话框中显示的提示信息
        redirectAttributes.addFlashAttribute("resultBean", resultBean);

        //重定向
        return "redirect:list";
    }
}
